﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.dto;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class GetDoctorHandler : IRequestHandler<GetDoctorCommand, APIPagging<DoctorDto>>
    {
        private readonly IBaseRepository<Doctor> doctorRepo;
        private readonly IBaseRepository<Work> workRepo;
        private readonly IBaseRepository<Office> officeRepo;
        private readonly IBaseRepository<Hospital> hosRepo;

        public GetDoctorHandler(IBaseRepository<Doctor> doctorRepo,IBaseRepository<Work> workRepo,IBaseRepository<Office> officeRepo,IBaseRepository<Hospital> hosRepo)
        {
            this.doctorRepo = doctorRepo;
            this.workRepo = workRepo;
            this.officeRepo = officeRepo;
            this.hosRepo = hosRepo;
        }
        /// <summary>
        /// 医生管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIPagging<DoctorDto>> Handle(GetDoctorCommand request, CancellationToken cancellationToken)
        {
            APIPagging<DoctorDto> res=new APIPagging<DoctorDto>();
            var doctor = doctorRepo.GetAll().Where(x => x.IsDel == false);
            var work = workRepo.GetAll();
            var office = officeRepo.GetAll();
            var hos = hosRepo.GetAll();
            var linq = from a in doctor
                       join b in office on a.OfficeId equals b.OfficeId
                       join c in work on a.WorkId equals c.WorkId
                       join d in hos on a.HospitalId equals d.HospitalId
                       select new DoctorDto
                       {
                           WorkId = c.WorkId,
                           WorkName = c.WorkName,
                           AuditStatus = a.AuditStatus,
                           DoctorId = a.DoctorId,
                           DoctorName = a.DoctorName,
                           IsDel = b.IsDel,
                           HospitalId = b.HospitalId,
                           HospitalName=d.HospitalName,
                           JoinTime = a.JoinTime,
                           OfficeId = b.OfficeId,
                           OfficeName = b.OfficeName,
                           Phone = a.Phone,
                           ServiceStatus = a.ServiceStatus,
                           Follower=a.Follower,
                       };
            if (!string.IsNullOrEmpty(request.DoctorName))
            {
                linq = linq.Where(x => x.DoctorName.Contains(request.DoctorName));
            }
            if (request.WorkId != 0)
            {
                linq = linq.Where(x => x.WorkId == request.WorkId);
            }
            if (request.OfficeId != 0)
            {
                linq = linq.Where(x => x.OfficeId == request.OfficeId);
            }
            res.TotaCount = await linq.CountAsync(cancellationToken);
            res.TotaSize = (int)Math.Ceiling(res.TotaCount * 1.0 / request.PageSize);
            res.PageData = await linq.OrderBy(x => x.DoctorId)
                                     .Skip((request.PageIndex - 1) * request.PageSize)
                                     .Take(request.PageSize)
                                     .ToListAsync(cancellationToken);
            res.Code = APIEnum.Success;
            return res;
        }
    }
}
